package insight.google.game.era.core;

import insight.google.game.era.bo.GameUser;
import insight.google.game.era.bo.Message;
import insight.google.game.era.client.proxy.ChatClientProxyImpl;
import insight.google.game.era.client.proxy.PlayerClientProxy;
import insight.google.game.era.client.proxy.PlayerClientProxyImpl;
import insight.google.game.era.client.proxy.RoomClientProxyImpl;
import insight.google.game.era.core.cache.AppCacheManager;
import insight.google.game.era.core.hub.HubServiceManager;
import insight.google.game.era.core.hub.PendingEvents;
import insight.google.game.era.core.hub.UserEvents;
import insight.google.game.era.data.User;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import net.sf.jsr107cache.Cache;

import org.directwebremoting.annotations.RemoteProperty;

import com.google.appengine.api.datastore.Key;


@PersistenceCapable(identityType = IdentityType.APPLICATION,detachable="true")
public class ChatCenter{

	public static final int HOST_GAMECENTER = 1;
	public static final int HOST_ROOM = 2;

	
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@PrimaryKey
	private Key id;

	@Persistent
	private String jsComponentId;


	//private ChatClientProxyImpl chatClientProxy;
	
	@Persistent
	private int chatCenterOwner;
	
	@Persistent
	private Key roomId;
	
	@Persistent
	private String roomName;

	@NotPersistent
	private Cache cache;

	// private LinkedHashMap<String, String> allMsg = new LinkedHashMap<String,
	// String>();
	@Persistent(defaultFetchGroup="true")
	private LinkedList<Message> allMsgList = new LinkedList<Message>();

	public ChatCenter(String jsComponentId, String roomName) {
		this.jsComponentId = jsComponentId;
		if (roomName != null) {
			this.chatCenterOwner = HOST_ROOM;
			this.roomName = roomName;
		} else {
			this.chatCenterOwner = HOST_GAMECENTER;
		}

//		ChatClientProxyImpl chatClientProxy = ChatClientProxyImpl.getInstance();
	}

	public void setJsComponentId(String jsComponentId) {
		this.jsComponentId = jsComponentId;

	}

	public boolean sendMessage(Message msg) {
		List<GameUser> userList = msg.getRecieverList();

		// allMsgList.add(msg);
		for (GameUser gameUser : userList) {
//			this.sendMessage(msg, gameUser);
			// chatClientProxy.showChatMessage(gameUser.getScriptProxy(),
			// jsComponentId, msg);
		}
		return true;
	}

	// public boolean sendMessage(Message msg, GameUser reciever, String
	// jsComponentId){
	//		
	// chatClientProxy.showChatMessage(reciever.getScriptProxy(), jsComponentId,
	// msg);
	// return true;
	// }
	//	
	public boolean sendMessage(Message msg, User reciever) {
		this.addMessage(msg);
		if(reciever.getScriptProxy() != null){
			ChatClientProxyImpl.getInstance().showChatMessage(reciever.getScriptProxy(), jsComponentId,msg);
		}else{
//			//pending
//			PendingEvents pendingEvent = new PendingEvents();
//			pendingEvent.setData(msg);
//			pendingEvent
//					.setFunctionName(UserEvents.CHATPROXY_SENDMSG
//							.toString());
//			pendingEvent.setJsComponentId(this.jsComponentId);
//			HubServiceManager.getInstance().addPendingEvent(reciever, pendingEvent);
		}
		
		return true;
	}

	public void addMessage(Message msg) {
		allMsgList.add(msg);
	}

	public LinkedList<Message> getAllMessages() {
		return allMsgList;
	}

	public Key getId() {
		return id;
	}

	public void setId(Key id) {
		this.id = id;
	}

	public String getJsComponentId() {
		return jsComponentId;
	}
	
	

}
